package com.cj.st.web;

import com.cj.st.entity.UserInfo;
import com.cj.st.sevice.UserInfoService;
import com.cj.st.util.ReturnT;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation(value = "获取用户列表", notes = "")
    @RequestMapping(value = "/userList", method = RequestMethod.GET)
    @RequiresPermissions("userInfo:view")//权限管理;
    public List<UserInfo> userInfo() {
        return userInfoService.getAll();
    }

    @ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
    @ApiImplicitParam(name = "userInfo", value = "用户详细实体userInfo", required = true, dataType = "UserInfo")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @RequiresPermissions("userInfo:add")//权限管理;
    public ReturnT saveUser(@RequestBody UserInfo userInfo) {
        return userInfoService.save(userInfo);
    }

    @ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/getById/{id}", method = RequestMethod.GET)
    @RequiresPermissions("userInfo:getById")//权限管理;
    public UserInfo findById(@PathVariable Integer id) {
        return userInfoService.findOne(id);
    }


    @ApiOperation(value = "更新用户详细信息", notes = "根据url的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParam(name = "userInfo", value = "用户详细实体userInfo", required = true, dataType = "UserInfo")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @RequiresPermissions("userInfo:update")//权限管理;
    public ReturnT putUser(@RequestBody UserInfo userInfo) {
        return userInfoService.save(userInfo);
    }

    @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @RequiresPermissions("userInfo:delete")//权限管理;
    public ReturnT deleteUser(@PathVariable Integer id) {
        return userInfoService.delete(id);
    }
}

